import time
import json
from alg import SortAlgorithm

def test_sort_algorithm(algorithm_name="bubble_sort"):
    # 读取测试数据
    with open("Group1-homework1\\data\\test_data.json", "r") as f:
     test_data = json.load(f)

    
    # 获取排序函数
    sort_func = getattr(SortAlgorithm, algorithm_name)
    
    # 对随机数组进行排序
    random_arr = test_data["random"].copy()
    
    # 计时开始
    start_time = time.time()
    
    # 执行排序并获取步骤
    sorted_arr, sort_steps = SortAlgorithm.bubble_sort(random_arr)
    
    # 计时结束
    end_time = time.time()
    execution_time = end_time - start_time
    
    # 验证排序结果
    is_correct = sorted_arr == sorted(random_arr)
    
    # 输出结果
    print(f"排序算法: {algorithm_name}")
    print(f"排序正确: {'是' if is_correct else '否'}")
    print(f"耗时: {execution_time:.6f}秒")
    
    # 保存日志（包含排序结果和步骤）
    SortAlgorithm.save_log(algorithm_name, sorted_arr, execution_time, steps=sort_steps)

if __name__ == "__main__":
    test_sort_algorithm("bubble_sort")